﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Autodesk.Revit.UI;

namespace HYRElectric.SystemDiagram
{
	public partial class AddLoopForm : System.Windows.Forms.Form
	{
		public string PhaseSequence
		{
			get
			{
				return this.cmbPhaseSequence.Text;
			}
		}

		public new string Load
		{
			get
			{
				return this.tbLoad.Text;
			}
		}

		public string Coefficient
		{
			get
			{
				return this.tbCoefficient.Text;
			}
		}

		public string Power
		{
			get
			{
				return this.tbPower.Text;
			}
		}

		public string Use
		{
			get
			{
				return this.cmbUse.Text;
			}
		}

		public string NumLoop
		{
			get
			{
				return this.numLoop.Text;
			}
		}

		public AddLoopForm(string title)
		{
			this.InitializeComponent();
			this.InitDlgData(title);
			this.numLoop.TextChanged += this.numLoop_TextChanged;
		}

		private void InitDlgData(string title)
		{
			this.Text = title;
			this.cmbPhaseSequence.Items.Add("三相");
			this.cmbPhaseSequence.Items.Add("单相");
			this.cmbPhaseSequence.Items.Add("L1");
			this.cmbPhaseSequence.Items.Add("L2");
			this.cmbPhaseSequence.Items.Add("L3");
			this.cmbPhaseSequence.SelectedIndex = 1;
			this.tbLoad.Text = "1.0";
			this.tbCoefficient.Text = "0.8";
			this.tbPower.Text = "0.8";
			this.cmbUse.Items.Add("照明");
			this.cmbUse.Items.Add("插座");
			this.cmbUse.SelectedIndex = 0;
			if ("编辑回路" == title)
			{
				this.lbLoop.Hide();
				this.numLoop.Hide();
				this.btnOk.Location = new System.Drawing.Point(this.btnOk.Location.X, this.lbLoop.Location.Y);
				this.btnCancel.Location = new System.Drawing.Point(this.btnCancel.Location.X, this.lbLoop.Location.Y);
				base.Size = new Size(base.Size.Width, base.Size.Height - 30);
				return;
			}
			this.numLoop.Text = "1";
		}

		private void AddLoopForm_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
			{
				base.Close();
			}
		}

		private void tbLoad_Leave(object sender, EventArgs e)
		{
			try
			{
				if (double.Parse(this.tbLoad.Text) <= 0.0)
				{
					throw new Exception();
				}
			}
			catch
			{
				TaskDialog.Show("盈建科", "负载值应为正数！");
				this.tbLoad.Text = this.m_load;
				this.tbLoad.Focus();
			}
			this.m_load = this.tbLoad.Text;
		}

		private void tbCoefficient_Leave(object sender, EventArgs e)
		{
			try
			{
				double num = double.Parse(this.tbCoefficient.Text);
				if (num <= 0.0 || num > 1.0)
				{
					throw new Exception();
				}
			}
			catch
			{
				TaskDialog.Show("盈建科", "需用系数值在0~1之间！");
				this.tbCoefficient.Text = this.m_coefficient;
				this.tbCoefficient.Focus();
			}
			this.m_coefficient = this.tbCoefficient.Text;
		}

		private void tbPower_Leave(object sender, EventArgs e)
		{
			try
			{
				double num = double.Parse(this.tbPower.Text);
				if (num <= 0.0 || num > 1.0)
				{
					throw new Exception();
				}
			}
			catch
			{
				TaskDialog.Show("盈建科", "功率因数值在0~1之间！");
				this.tbPower.Text = this.m_power;
				this.tbPower.Focus();
			}
			this.m_power = this.tbPower.Text;
		}

		private void numLoop_TextChanged(object sender, EventArgs e)
		{
			try
			{
				double num = double.Parse(this.numLoop.Text);
				if (num <= 0.0 || num > 100.0)
				{
					throw new Exception();
				}
			}
			catch
			{
				TaskDialog.Show("盈建科", "回路数值在0~100之间！");
				this.numLoop.Text = this.m_numLoop;
				this.numLoop.Focus();
			}
			this.m_numLoop = this.numLoop.Text;
		}

		private void tbLoad_KeyPress(object sender, KeyPressEventArgs e)
		{
			this.CheckInputType(e, this.tbLoad.Text);
		}

		private void tbCoefficient_KeyPress(object sender, KeyPressEventArgs e)
		{
			this.CheckInputType(e, this.tbCoefficient.Text);
		}

		private void tbPower_KeyPress(object sender, KeyPressEventArgs e)
		{
			this.CheckInputType(e, this.tbPower.Text);
		}

		private void CheckInputType(KeyPressEventArgs e, string txt)
		{
			if ((e.KeyChar < '0' || e.KeyChar > '9') && e.KeyChar != '\b' && e.KeyChar != '\r' && e.KeyChar != '.')
			{
				e.Handled = true;
			}
			if (e.KeyChar == '.' && txt.IndexOf(".") >= 0)
			{
				e.Handled = true;
			}
		}

		private string m_load = "1.0";

		private string m_coefficient = "0.8";

		private string m_power = "0.8";

		private string m_numLoop = "1";
	}
}
